/*
	About: Description
		This file contains controllers concerned with news.
*/

exports.controllers={
	/*
		Function: controllers.news

		Parameters:
			options - an object of:
			options.feed_id - optional, id from Tnews_feeds table
			options.feed_code - optional, code from Tnews_feeds table
			options.feed_name - optional, name from Tnews_feeds table
			options.template_path - optional, template path, excluding widgets/, defaults to 'news/'
		Returns:
			space.output and Valid HTML
	*/
	
	news: function(space,options) {
		if (!options) options={};
		space.options=options;
		if (!options.template_path) options.template_path="news";
		if (space.fields.news_code) {
			space.news=space.mfact.list("news",{calc_code: space.fields.news_code,enabled:1})[0];
			if (!space.news) {
				return space.output=space.views.process("widgets/"+options.template_path+"/view_none",space);
			}
			space.pageinfo.title=space.views.process("widgets/"+options.template_path+"/view_title",space);
			if (space.news.image_group_id) {
				space.news_images={
					folder_id: space.news.image_group_id
				};
			}
			return space.output=space.views.process("widgets/"+options.template_path+"/view",space);
		}
		if (space.fields.tag) {
			return "";
		}
		var nfid=space.functions.get_news_feed_id(space,options);
		var h={enabled:1};
		if (nfid) h.feed_id=nfid;
		if (options.pagesize) {
			var cnt=space.mfact.count("news",h);
			h.order_by="main.date_use desc";
			h.limit=options.pagesize*1;
			var p=(space.fields.page||0)*1;
			h.offset=options.pagesize*p;
			space.news=space.mfact.list("news",h);
			if (p) {
				if (p==1) space.prev_page=space.action; else space.prev_page=space.action+"?page="+(p-1);
			}
			if (p+1<cnt/options.pagesize) {
				space.next_page=space.action+"?page="+(p+1);
			}
		} else {
			h.order_by="main.date_use desc";
			space.news=space.mfact.list("news",h);
		}
		return space.output=space.views.process("widgets/"+options.template_path+"/list",space);
	},
	/*
		Function: controllers.news_short

		Parameters:
			options - an object of:
			options.feed_id - optional, id from Tnews_feeds table
			options.feed_code - optional, code from Tnews_feeds table
			options.feed_name - optional, name from Tnews_feeds table
			options.template - optional, template to render, defaults to "news/short"
			options.limit - optional, number of news to show, defults to 3
			options.offset - optional, number of news to skip, defults to 0
		Returns:
			Valid HTML
	*/
	news_short: function(space,options) {
		if (!options) options={};
		space.options=options;
		var nfid=space.functions.get_news_feed_id(space,options);
		var filter={order_by:"main.date_use desc",limit: options.limit||3,offset: options.offset||0};
		if (nfid) filter.feed_id=nfid;
		space.news_short=space.mfact.list("news",filter);
		return space.views.process("widgets/"+(options.template||"news/short"),space);
	}
};

exports.functions={
	get_news_feed_id:function(space,options,prefix) {
		if (!prefix) prefix="feed";
		if (options[prefix+"_id"]) return options[prefix+"_id"];
		if (options[prefix+"_code"]) return space.mfact.get("news_feed",{code: options[prefix+"_code"]}).id;
		if (options[prefix+"_name"]) return space.mfact.get("news_feed",{name: options[prefix+"_name"]}).id;
		return undefined;
	}
};

